package com.mc.dao.Impl;

import com.mc.bean.Grade;
import com.mc.dao.GradeDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zh
 * 年级实体类数据访问接口实现
 */
public class GradeDaoImpl extends BaseDao implements GradeDao {


    @Override
    public List<Grade> findAll() {
        List<Grade> list = new ArrayList<>();
        try {
            //1.注册驱动
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql = "select * from Grade";
            super.getPst(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                //创建对象
                Grade grade = new Grade(rs.getInt(1), rs.getString(2));
                //将对象放入集合中
                list.add(grade);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public Grade findByGid(int gid) {
        Grade grade = null;
        try {
            //1.注册驱动
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql = "select * from Grade where gid=?";
            super.getPst(sql);
            super.pstmt.setInt(1, gid);
            getRs();
            while (rs.next()){
                grade=new Grade(rs.getInt(1),rs.getString(2));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return grade;
    }

}